package com.roadjava.GDOUGym.service.impl;


import com.roadjava.GDOUGym.bean.dto.CompetitionDTO;
import com.roadjava.GDOUGym.bean.entity.CompetitionDO;
import com.roadjava.GDOUGym.bean.entity.CompetitionTeamDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.CompetitionConvert;
import com.roadjava.GDOUGym.mapper.CompetitionMapper;
import com.roadjava.GDOUGym.mapper.CompetitionTeamMapper;
import com.roadjava.GDOUGym.service.CompetitionService;
import com.roadjava.GDOUGym.service.CompetitionTeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CompetitionTeamServiceImpl implements CompetitionTeamService {
    @Resource
    private CompetitionTeamMapper competitionTeamMapper;


    @Override
    public ResultDTO<List<CompetitionTeamDO>> find(CompetitionTeamDO competitionTeamDO) {

        List<CompetitionTeamDO> dos = competitionTeamMapper.find(competitionTeamDO);
        long total = competitionTeamMapper.selectCount(competitionTeamDO);
        return ResultDTO.buildSuccess(dos,total);
    }

    @Override
    public ResultDTO<String> add(CompetitionTeamDO competitionTeamDO) {
        CompetitionTeamDO competitionTeamDO1 = new CompetitionTeamDO();

        String no;

        List<CompetitionTeamDO> competitionDOList = competitionTeamMapper.find(competitionTeamDO1);
        System.out.println("competitionDOList = " + competitionDOList);
        if(competitionDOList.size() == 0){
            no = "0001";
        }else{
            String endNo = competitionDOList.get(competitionDOList.size()-1).getTeamNo();
            long total = Integer.parseInt(endNo)+1;

            no = total+"";
            if(total<10){
                no = "000" + no;
            }else if(total<100){
                no = "00" + no;
            }else if(total < 1000){
                no = "0" + no;
            }
        }

        // 把dto转为do对象
        System.out.println(no);
        competitionTeamDO.setTeamNo(no);
        System.out.println("competitionTeamDO = " + competitionTeamDO);
        int result = competitionTeamMapper.add(competitionTeamDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("申请成功");
        }else {
            return ResultDTO.buildSuccess("申请失败");
        }
    }

    @Override
    public ResultDTO<String> change(CompetitionTeamDO competitionTeamDO) {
        // 把dto转为do对象
        int result = competitionTeamMapper.change(competitionTeamDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(CompetitionTeamDO competitionTeamDO) {
        // 把dto转为do对象
        int result = competitionTeamMapper.delete(competitionTeamDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("删除成功");
        }else {
            return ResultDTO.buildSuccess("删除失败");
        }
    }
}
